package com.lsp.base.empty;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.lsp.base.R;
import com.lsp.base.R2;
import com.lsp.lib_toolkit.NetworkUtils;
import com.lsp.lib_toolkit.ViewUtils;

/**
 * 该view是用于页面首次进入时候的loading页面 以及加载数据之后的成功，失败，无数据等各种状态显示的
 *
 * @author com.lsp.ibxs
 * @created 16/11/25 11:15
 */
public class EmptyLayout extends LinearLayout {

  /**
   * 网络异常
   */
  public static final int NETWORK_ERROR = 0;
  /**
   * 正在加载
   */
  public static final int NETWORK_LOADING = 1;
  /**
   * 无数据页面
   */
  public static final int NODATA = 2; // 无数据页面

  /**
   * 加载结束
   */
  public static final int HIDE_LAYOUT = 3;
  private int mErrorState;

  @BindView(R2.id.iv_loading_image) ImageView ivLoadingImage;
  @BindView(R2.id.iv_loaded_image) ImageView ivLoadedImage;
  @BindView(R2.id.tv_loaded_text) TextView tvLoadedText;
  @BindView(R2.id.btn_refresh) Button btnRefresh;
  @BindView(R2.id.ll_loaded_layout) LinearLayout llLoadedLayout;
  @BindView(R2.id.empty_view_layout) RelativeLayout emptyViewLayout;

  protected boolean clickEnable = true;
  private Context mContext;
  private EmptyModel emptyModel;

  private OnClickListener buttonClickListener;

  public EmptyLayout(Context context) {
    this(context, null);
    mContext = context;
  }

  public EmptyLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
    init();
  }

  private void init() {
    LayoutInflater.from(mContext).inflate(R.layout.empty_layout, this);
    ButterKnife.bind(this);
    ViewUtils.startViewAnimation(ivLoadingImage);
    btnRefresh.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View v) {
        if (clickEnable) {
          if (buttonClickListener != null) {
            buttonClickListener.onClick(v);
          }
        }
      }
    });
    useDefaultEmptyModel();
  }

  private void useDefaultEmptyModel() {
    new EmptyModel(R.mipmap.icon_empty_view_load_error_common,
        R.string.empty_view_load_error_common, R.mipmap.icon_empty_view_no_data_common,
        R.string.empty_view_no_data_common, R.mipmap.icon_empty_view_no_network_common,
        R.string.empty_view_no_network_common);
  }

  public void setErrorType(int errorType) {
    if (emptyModel == null) {
      return;
    }
    setVisibility(VISIBLE);
    switch (errorType) {
      //加载出错
      case NETWORK_ERROR:
        mErrorState = NETWORK_ERROR;
        ivLoadingImage.setVisibility(GONE);
        llLoadedLayout.setVisibility(VISIBLE);
        if (NetworkUtils.isNetworkAvailable(mContext)) {
          ivLoadedImage.setImageResource(emptyModel.getLoadErrorImageResId());
          tvLoadedText.setText(emptyModel.getLoadErrorTextResId());
        } else {
          ivLoadedImage.setImageResource(emptyModel.getNoNetworkImageResId());
          tvLoadedText.setText(emptyModel.getNoNetworkTextResId());
        }
        // 显示按钮
        btnRefresh.setVisibility(buttonClickListener != null ? VISIBLE : GONE);
        clickEnable = true;
        break;
      //正在加载中
      case NETWORK_LOADING:
        mErrorState = NETWORK_LOADING;
        ivLoadingImage.setVisibility(View.VISIBLE);
        llLoadedLayout.setVisibility(GONE);
        clickEnable = false;
        break;
      //加载无数据
      case NODATA:
        mErrorState = NODATA;
        ivLoadingImage.setVisibility(GONE);
        llLoadedLayout.setVisibility(VISIBLE);
        ivLoadedImage.setImageResource(emptyModel.getNoDataImageResId());
        tvLoadedText.setText(emptyModel.getNoDataTextResId());
        //btnRefresh.setVisibility(listener != null ? VISIBLE : GONE);
        btnRefresh.setVisibility(GONE);
        clickEnable = true;
        break;
      //加载成功
      case HIDE_LAYOUT:
        setVisibility(GONE);
        break;
      default:
        break;
    }
  }

  public void setButtonClickListener(OnClickListener buttonClickListener) {
    this.buttonClickListener = buttonClickListener;
  }

  public void setNoDataResIds(@StringRes int noDataTextResId, @DrawableRes int noDataImageResId) {
    if (emptyModel != null) {
      emptyModel.setNoDataTextResId(noDataTextResId);
      emptyModel.setNoDataImageResId(noDataImageResId);
    }
  }

  public void setNoNetworkResIds(@StringRes int noNetworkTextResId,
      @DrawableRes int noNetworkImageResId) {
    if (emptyModel != null) {
      emptyModel.setNoNetworkTextResId(noNetworkTextResId);
      emptyModel.setNoNetworkImageResId(noNetworkImageResId);
    }
  }

  public void setLoadErrorResIds(@StringRes int errorDataTextResId,
      @DrawableRes int errorDataImageResId) {
    if (emptyModel != null) {
      emptyModel.setLoadErrorTextResId(errorDataTextResId);
      emptyModel.setLoadErrorImageResId(errorDataImageResId);
    }
  }
}
